package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.NeedChangeFragmentBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseFragComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseFragModule;
import com.sgb.kjwl.model.entity.Bean;
import com.sgb.kjwl.model.entity.enterprise.ListBean;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.swgk.core.util.UiUtil;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LoginEvent;

import com.sgb.kjwl.view.ui.activity.enterprise.NewNeedDetailsActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.NeedAdapter;
import com.sgb.kjwl.viewmodel.enterprise.NeedChangeModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class NeedChangeFragment extends AppFragment {

    @Inject
    NeedChangeModel mModel;
    private NeedChangeFragmentBinding mBinding;
    private int mIndexPage = 1;
    private NeedAdapter mNeedAdapter;
    private boolean isLoadMore;
    private int type;

    @Override
     protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerEnterpriseFragComponent.builder().appComponent(appComponent)
                .enterpriseFragModule(new EnterpriseFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.need_change_fragment, container, false);
        return mBinding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
        initData();
        initListener();
    }

    private void initData() {

    }

    private void initListener() {
        mBinding.rlCategory.setOnClickListener(this);
        mBinding.rlCity.setOnClickListener(this);
        mBinding.rlPrice.setOnClickListener(this);
        mBinding.rlSendtype.setOnClickListener(this);
        mNeedAdapter.setOnItemClick(new AdapterOnItemClick<ListBean>() {
            @Override
            public void onItemClick(ListBean listBean, int position) {
                NewNeedDetailsActivity.startactivity(getActivity(), listBean.id + "");
            }
        });

        mBinding.displayView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                setLoadMore(false);
            }

            @Override
            public void onLoadMore() {
                setLoadMore(true);
            }
        });

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    private void showPop() {

    }

    private void initView() {
        type = getArguments().getInt("type");
        mNeedAdapter = new NeedAdapter(getActivity(), null, false);
        mBinding.displayView.setLayoutManager(new LinearLayoutManager(UiUtil.getContext()));
        mBinding.displayView.setLoadingMoreEnabled(true);
        mBinding.displayView.setAdapter(mNeedAdapter);

        mNeedAdapter.setmAskClick(item -> {
            if (DialogHelper.checkLogin()) {
                DialogHelper.showDialog(getContext(), "确认呼叫", item.linkMan + "：" + item.linkmanPhone, "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        ActivityJump.jumpToPhone(getContext(), item.linkmanPhone);
                    }
                });
            } else {
                DialogHelper.showLoginDialogNew(getContext(), EventConstant.EVENT_NEED_FRG_JUMP, "温馨提示", "登录后可拨打电话,去登录", "登录");
            }
        });
    }

    // 获取商标类型选项
    public void getTrademarkTypeOption(List<Bean> data) {

    }

    // 行业分类
    public void getAllSserviceCategory(List<Bean> data) {
    }

    // 获取领域分类
    public void getAllCopyright(List<Bean> data) {

    }

    // 获取所有版权
    public void getAllQualification(List<Bean> data) {

    }


    public void getPatentTypeOption(List<Bean> data) {

    }

    // 需求大厅列表
    public void getDemandHallList(List<ListBean> data) {
        mBinding.displayView.refreshComplete();
        if (mIndexPage == 1) {
            mBinding.displayView.setVisibility(data.size() != 0 ? View.VISIBLE : View.GONE);
            mBinding.errorView.setVisibility(data.size() != 0 ? View.GONE : View.VISIBLE);
            mNeedAdapter.setDatas(data);
            mBinding.displayView.setLoadingMoreEnabled(true);
        } else {
            if (data.size() != 0) {
                mBinding.displayView.loadMoreComplete();
                mNeedAdapter.addDatas(data);
            } else {
                mBinding.displayView.setNoMore(true);
            }
        }
        mNeedAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LoginEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_NEED_FRG_JUMP) {
            setLoadMore(false);
        }
    }

    public void setLoadMore(boolean isLoadMore){
        /**
         *商标分类
         *最低价
         *最高价
         *领域分类编号
         *专利类型
         *专利状态  1 授权已下证 0授权未下证
         *版权分类编号版权授权方式 1独家授权 2非独家授权
         *授权方式 1独家授权 2非独家授权
         *行业类型编号
         *企业类型编号企业类型名称:0:有限责任公司;1:个人独资公司;2:港澳台公司,3:集团公司
         *纳税规模 0：小规模:1：一般纳税人:2：出口纳税人:3：未核税；
         */
        mIndexPage = isLoadMore ? mIndexPage+1 : 1;
        Map<String,Object> map = new HashMap<>();
        map.put("requirementTypes",type+"");
        map.put("categoryNo","");
        map.put("minPrice","");
        map.put("maxPrice","");
        map.put("pageSize",mIndexPage);
        map.put("limit",20);
        map.put("areaClassification","");
        map.put("patentType","");
        map.put("patentStatus","");
        map.put("classificationCopyright","");
        map.put("authorizationWay","");
        map.put("industryTypeId","");
        map.put("enterpriseTypeId","");
        map.put("taxScale","");
        mModel.getDemandHallList(map);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.rl_category:
                mBinding.rlCategory.contentSelect(false,true);
                break;
            case R.id.rl_city:
                mBinding.rlCity.contentSelect(false,true);
                break;
            case R.id.rl_price:
                mBinding.rlPrice.contentSelect(false,true);
                break;
            case R.id.rl_sendtype:
                mBinding.rlSendtype.contentSelect(false,true);
                break;
        }
    }

    private void oneClick(){
        switch (type){
            case 1:
                
                break;
            case 2:

                break;
            case 3:

                break;
            case 4:

                break;
        }
    }
}
